//
//  ChagePasswordViewController.m
//  JianChaRiBao
//
//  Created by Changxy on 13-7-29.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ChagePasswordViewController.h"
#import "AppDelegate.h"
@interface ChagePasswordViewController ()

@end

@implementation ChagePasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(modifyResult:) name:Kmodify object:nil];
    }
    return self;
}
- (IBAction)backButtonClicked:(id)sender {
    [self resign];
    [[UIApplication rootNavController] popViewControllerAnimated:YES];
}
-(void)modifyResult:(NSNotification *)notifi{
    NSString * str = [notifi object];
    if ([str isEqualToString:@"true"]) {
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"修改成功"];
        [abp show];
        [abp release];
        [[UIApplication rootNavController] popToRootViewControllerAnimated:YES];
    }else{
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"修改失败"];
        [abp show];
        [abp release];

    }
}
-(BOOL)isValidatePassworld:(NSString *)passworld{
    NSString * usernameRegex = @"[A-Z0-9a-z]{6,16}";
    
    NSPredicate *userText = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",usernameRegex];
    
    return [userText evaluateWithObject:passworld];
}

- (IBAction)submitButtonClicked:(id)sender {
    [self resign];
    if (confirmPassworld.text && originalPassworld.text && newPassworld.text) {
        if ([self isValidatePassworld:newPassworld.text]) {
            if ([newPassworld.text isEqualToString:confirmPassworld.text]) {
                [[AuthorManager defaultManager]modifyPasswordWithOriginPassword:originalPassworld.text newPassword:newPassworld.text];
            }else{
                ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"两次密码不一致"];
                [abp show];
                [abp release];
            }

        }else{
            ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"输入密码非法"];
            [abp show];
            [abp release];
        }

    }else{
        ABPromptView * abp = [[ABPromptView alloc]initWithTitle:@"请填写完整"];
        [abp show];
        [abp release];

    }

    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    originalPassworld.delegate = self;
    newPassworld.delegate = self;
    confirmPassworld.delegate = self;
    shouldSwipeBack = YES;
    
}
-(void)resign{
    [originalPassworld resignFirstResponder];
    [newPassworld resignFirstResponder];
    [confirmPassworld resignFirstResponder];

}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self resign];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [originalPassworld release];
    [newPassworld release];
    [confirmPassworld release];
    [super dealloc];
}
- (void)viewDidUnload {
    [originalPassworld release];
    originalPassworld = nil;
    [newPassworld release];
    newPassworld = nil;
    [confirmPassworld release];
    confirmPassworld = nil;
    [super viewDidUnload];
}
@end
